<?php

namespace app\controller\admin\sky;

use app\model\api\SkyCategory;
use laytp\controller\Backend;
use laytp\library\CommonFun;

/**
 * Class Author
 * @package app\controller\admin\peace
 */
class Season extends Backend
{
    /**
     * api_log模型对象
     * @var \app\model\api\Log
     */
    protected $model;
    protected $hasSoftDel=0;//是否拥有软删除功能

    protected $noNeedLogin = []; // 无需登录即可请求的方法
    protected $noNeedAuth = ['index', 'info']; // 无需鉴权即可请求的方法


    public function _initialize()
    {
        $this->model = new \app\model\api\SkySeason();
    }


    public function season()
    {
        $where   = $this->buildSearchParams();
        $order   = $this->buildOrder();
        $data = (new SkyCategory())->where(['category'=>'季节先祖'])->order($order)->cache(true)->select()->toArray();
        return $this->success('数据获取成功', $data);
    }


    //添加
    public function add()
    {
        try {
            $post     = $this->request->post();
            if(empty($post['time'])){
                throw new \Exception('请选择日期');
            }
            $all_date = explode(' - ',$post['time']);

            $saveData = [
                'start_date'    => strtotime(trim($all_date[0])),
                'end_date'      => strtotime(trim($all_date[1])),
                'season'        => $post['season'],
                'bg_img'        => $post['bg_img'],
                'date_bg_color' => $post['date_bg_color'],
                'bg_color'      => $post['bg_color'],
                'word_color'    => $post['word_color'],
                'guide_img'     => json_encode(array(
                    ['icon'=>$post['guide_img1'],'name'=> $post['guide_title1']],
                    ['icon'=>$post['guide_img2'],'name'=> $post['guide_title2']],
                    ['icon'=>$post['guide_img3'],'name'=> $post['guide_title3']],
                    ['icon'=>$post['guide_img4'],'name'=> $post['guide_title4']],
                ),JSON_UNESCAPED_UNICODE),
                'tag_icon0'     => $post['tag_icon0'],
                'tag1'          => $post['tag1'],
                'tag_icon1'     => $post['tag_icon1'],
                'video_cover1'  => $post['video_cover1'],
                'content1'      => strip_tags($post['content1'],'<p><img><video>'),
                'tag2'          => $post['tag2'],
                'tag_icon2'     => $post['tag_icon2'],
                'video_cover2'  => $post['video_cover2'],
                'content2'      => strip_tags($post['content2'],'<p><img><video>'),
                'tag3'          => $post['tag3'],
                'tag_icon3'     => $post['tag_icon3'],
                'video_cover3'  => $post['video_cover3'],
                'content3'      => strip_tags($post['content3'],'<p><img><video>'),
                'tag4'          => $post['tag4'],
                'tag_icon4'     => $post['tag_icon4'],
                'video_cover4'  => $post['video_cover4'],
                'content4'      => strip_tags($post['content4'],'<p><img><video>'),
                'tag5'          => $post['tag5'],
                'tag_icon5'     => $post['tag_icon5'],
                'video_cover5'  => $post['video_cover5'],
                'content5'      => strip_tags($post['content5'],'<p><img><video>'),
            ];

            $saveAll = $this->model->save($saveData);
            if (!$saveAll) throw new \Exception('保存失败');

            $this->clearCache();
            return $this->success('操作成功');
        } catch (\Exception $e) {
            return $this->exceptionError($e);
        }
    }


    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit()
    {
        $id = $this->request->param('id');
        $post = CommonFun::filterPostData($this->request->post());
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');

        if(empty($post['time'])){
            throw new \Exception('请选择日期');
        }
        $all_date = explode(' - ',$post['time']);
        $row->start_date    = strtotime(trim($all_date[0]));
        $row->end_date      = strtotime(trim($all_date[1]));
        $row->season        = $post['season'];
        $row->bg_img        = $post['bg_img'];
        $row->date_bg_color = $post['date_bg_color'];
        $row->bg_color      = $post['bg_color'];
        $row->word_color    = $post['word_color'];
        $row->guide_img     = json_encode(array(
            ['icon'=>$post['guide_img1'],'name'=> $post['guide_title1']],
            ['icon'=>$post['guide_img2'],'name'=> $post['guide_title2']],
            ['icon'=>$post['guide_img3'],'name'=> $post['guide_title3']],
            ['icon'=>$post['guide_img4'],'name'=> $post['guide_title4']],
        ),JSON_UNESCAPED_UNICODE);
        $row->tag_icon0     = $post['tag_icon0'];
        $row->tag1          = $post['tag1'];
        $row->tag_icon1     = $post['tag_icon1'];
        $row->video_cover1  = $post['video_cover1'];
        $row->content1      = strip_tags($post['content1'],'<p><img><video>');
        $row->tag2          = $post['tag2'];
        $row->tag_icon2     = $post['tag_icon2'];
        $row->video_cover2  = $post['video_cover2'];
        $row->content2      = strip_tags($post['content2'],'<p><img><video>');
        $row->tag3          = $post['tag3'];
        $row->tag_icon3     = $post['tag_icon3'];
        $row->video_cover3  = $post['video_cover3'];
        $row->content3      = strip_tags($post['content3'],'<p><img><video>');
        $row->tag4          = $post['tag4'];
        $row->tag_icon4     = $post['tag_icon4'];
        $row->video_cover4  = $post['video_cover4'];
        $row->content4      = strip_tags($post['content4'],'<p><img><video>');
        $row->tag5          = $post['tag5'];
        $row->tag_icon5     = $post['tag_icon5'];
        $row->video_cover5  = $post['video_cover5'];
        $row->content5      = strip_tags($post['content5'],'<p><img><video>');
        try {
            $save = $row->save();
            if ($save === false) throw new \Exception('保存失败');
            $this->clearCache();
            return $this->success('保存成功');
        } catch (\Exception $e) {
            return $this->error($e->getMessage());
        }
    }


    /**
     * 获取详情
     * @return false|string|\think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function info(){
        $id = $this->request->param('id');

        $info = $this->model->where('id',$id)->find()->toArray();
        $guide = json_decode($info['guide_img'],true);
        for($i=1;$i<=4;$i++){
            $info['guide_img'.$i] = !empty($guide[$i-1]['icon'])?$guide[$i-1]['icon']:'';
            $info['guide_title'.$i] = !empty($guide[$i-1]['name'])?$guide[$i-1]['name']:'';
        }

        if(!empty($info['start_date'])){
            $info['time'] = date('Y-m-d',$info['start_date']).' - '.date('Y-m-d',$info['end_date']);
        }

        return $this->success('获取成功', $info);
    }



    //设置当前季节
    public function setStatus()
    {
        $id       = $this->request->post('id');
        $fieldVal = $this->request->post('field_val');
        $isRecycle = $this->request->post('is_recycle');
        $update['status'] = $fieldVal;

        try {

            $check = $this->model->where('status','=',1)->count();
            if($check && $fieldVal){
                return $this->error('请先取消其他季节后再设置');
            }

            if($isRecycle) {
                $updateRes = $this->model->onlyTrashed()->where('id', '=', $id)->update($update);
            } else {
                $updateRes = $this->model->where('id', '=', $id)->update($update);
            }

            if ($updateRes) {
                $this->clearCache();
                return $this->success('操作成功');
            } else if ($updateRes === 0) {
                return $this->success('未作修改');
            } else {
                return $this->error('操作失败');
            }
        } catch (\Exception $e) {
            return $this->exceptionError($e);
        }
    }
}